package com.waypolice.common.ftp;

import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.MalformedURLException;

/**
 * 描述:
 *
 * @author zyp
 * @description 作为挂接使用ftp
 * @create 2019-06-22 14:08
 */
public class FTPClientForMount extends FTPBase {

    private static Logger logger = LoggerFactory.getLogger(FTPClientForMount.class);

    private String ftp_host;

    private String ftp_port;

    private String ftp_user;

    private String ftp_password;

    public FTPClientForMount(String ftp_host, String ftp_port, String ftp_user, String ftp_password) {
        this.ftp_host = ftp_host;
        this.ftp_port = ftp_port;
        this.ftp_user = ftp_user;
        this.ftp_password = ftp_password;
    }

    @Override
    public boolean initFTPClient() {
        boolean flag = false;
        logger.info("inno...挂接文件下载ftp连接");
        ftpClient = new FTPClient();
        ftpClient.setControlEncoding("GBK");
        logger.info("获取配置文件中的ftp设定..."+"down_ftp_host:"+ftp_host+" down_ftp_port:"+ftp_port
                +" down_ftp_user:"+ftp_user +" down_ftp_password:"+ftp_password);
        try {
            logger.info("内网下载ftp连接中...");
            ftpClient.connect(ftp_host, Integer.valueOf(ftp_port));
            ftpClient.login(ftp_user, ftp_password);
            ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
            int replyCode = ftpClient.getReplyCode();
            if(!FTPReply.isPositiveCompletion(replyCode)){
                logger.info("ftp连接失败！！！");
            }else{
                flag = true;
                logger.info("ftp连接成功！！！");
            }
        }catch(MalformedURLException e){
            e.printStackTrace();
        }catch (IOException e) {
            e.printStackTrace();
        }
        return flag;
    }

    /**
     * @author:zyp
     * @description 下载文件
     * @date: 2018/8/22 11:19
     * @return
     * fileName:下载的文件的文件名
     * pathname:现在服务器的文件地址
     * down_path:下载后放的地址
     */
    @Override
    public boolean downloadFile(String fileName, String pathname, String down_path) {
        logger.info("下载文件的名字:"+fileName+"；下载的文件路径："+pathname+"；下载后放入本地的地址"+down_path);
//        boolean isConnect = initFTPClient();
        boolean flag = false;
        OutputStream os = null;
        try {
            logger.info("----开始下载文件----");
            FTPFile[] ftpFiles = ftpClient.listFiles(pathname);
            if(ftpFiles.length>0){
                for (FTPFile file : ftpFiles) {
                    String FTPFileName = file.getName();
                    if(file.isDirectory()){
                        flag = downloadFile(fileName,pathname+"/"+FTPFileName,down_path);
                    }else if(file.isFile()){
                        if (fileName.equalsIgnoreCase(FTPFileName)) {
                            //切换目录
                            ftpClient.changeWorkingDirectory(pathname);
                            File localFile = new File(down_path + "/" + file.getName());
                            os = new FileOutputStream(localFile);
                            ftpClient.retrieveFile(file.getName(), os);
                            os.close();
                            flag = true;
                            ftpClient.logout();
                            logger.info("----下载文件成功----");
                        }
                    }
                    if (flag){
                        if (ftpClient.isConnected()) {
                            try {
                                ftpClient.disconnect();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                        return flag;
                    }
                }
            }
        } catch (Exception e) {
            logger.error("----下载文件失败----");
            e.printStackTrace();
        } finally {
            if (null != os) {
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        if (!flag){
            logger.info("----下载文件失败----");
        }
        return flag;
    }

    public String getFtp_host() {
        return ftp_host;
    }

    public void setFtp_host(String ftp_host) {
        this.ftp_host = ftp_host;
    }

    public String getFtp_port() {
        return ftp_port;
    }

    public void setFtp_port(String ftp_port) {
        this.ftp_port = ftp_port;
    }

    public String getFtp_user() {
        return ftp_user;
    }

    public void setFtp_user(String ftp_user) {
        this.ftp_user = ftp_user;
    }

    public String getFtp_password() {
        return ftp_password;
    }

    public void setFtp_password(String ftp_password) {
        this.ftp_password = ftp_password;
    }
}